import { CMCClient } from "mpj-cmc/cmc.client";
export class FrontNotifyClient {
    private client: CMCClient;
    get ClientId() { return this.client ? this.client.ClientId : ""; }
    get IsConnect(): boolean { return this.client && this.client.IsConnect; }
    constructor(options: FNCOptions) {
        this.client = new CMCClient(options.fnsHostName, options.fnsPort, options.clientId);
        this.client.onReceive = (msg: NoticeBase) => { 
            console.log("收到FNS的消息", msg);
            console.log("当前处理器列表： ===>", this.msgHandlerList);
            for (let i = 0; i < (<any>this.msgHandlerList).length; i++) {
                if (msg.type == this.msgHandlerList[i].notifyType) {
                    this.msgHandlerList[i].handler(msg.content);
                }
            }
        }
        this.client.onConnect = () => {
            //重连的时候重新注册消息处理器
            for (let item of this.msgHandlerList) {
                let msg: RegistHandler = new RegistHandler();
                msg.content = {
                    notifyType: item.notifyType
                };
                this.client.Send(msg);
            }

            this.onConnect && this.onConnect();
        }
        this.client.Connect();
    }
    private msgHandlerList: ({ notifyType: string, handler: (args) => void })[] = [];
    /**
     * 注册消息事件处理程序
     * @param  {string} notifyType
     * @param  {(args)=>void} handler
     */
    RegistMessageHandler(notifyType: string, handler: (args) => void) {
        let msg: RegistHandler = new RegistHandler();
        msg.content = {
            notifyType: notifyType
        };
        this.client.Send(msg);

        this.removeHanlder(notifyType);
        this.msgHandlerList.push({
            notifyType: notifyType,
            handler: handler
        });
        console.log("this.msgHandlerList ===>", this.msgHandlerList);
    }
    /**
     * 移除消息事件处理程序
     * @param  {string} notifyType
     */
    RemoveMessageHandler(notifyType: string) {
        let msg: RemoveHandler = new RemoveHandler();
        msg.content = {
            notifyType: notifyType
        };
        this.client.Send(msg);
        this.removeHanlder(notifyType);

    }
    private removeHanlder(notifyType: string) {
        for (let i = 0; i < this.msgHandlerList.length; i++) {
            if (this.msgHandlerList[i].notifyType == notifyType) {
                this.msgHandlerList.splice(i, 1);
            }
        }
    }
    /**
     * 通知消息
     * @param message 消息内容
     */
    Notify(message: NoticeBase) {
        let msg: NoticeBase = {
            type: "Notify",
            content: message
        };

        this.client.Send(msg);
    }
    /**
     * 断开连接
     */
    DisConnect(): Promise<boolean> {
        return new Promise((resolve, reject) => {
            this.client.DisConnect();
            this.client.onDisconnect = () => {
                resolve(true);
            }
        });
    }

    /**
     * 连接FNS成功出发的事件
     */
    onConnect: () => void;
}

interface NoticeBase {
    type: string;
    content: any;
}

class RegistHandler implements NoticeBase {
    type: string;
    content: any;
    constructor() {
        this.type = "RegisHandler";
    }
}

class RemoveHandler implements NoticeBase {
    type: string;
    content: any;
    constructor() {
        this.type = "RemoveHandler";
    }
}

interface FNCOptions {
    fnsHostName: string,
    fnsPort: number,
    clientId: string
}